package com.basic.business.order.service.impl;

import com.basic.business.common.utils.ULIDUtils;
import com.basic.business.order.domain.TOrdersDetail;
import com.basic.business.order.domain.TOrdersMain;
import com.basic.business.order.domain.TOrdersMerge;
import com.basic.business.order.domain.TOrdersTimeLine;
import com.basic.business.order.mapper.ITOrdersDetailMapper;
import com.basic.business.order.mapper.ITOrdersMainMapper;
import com.basic.business.order.mapper.ITOrdersMergeMapper;
import com.basic.business.order.mapper.ITOrdersTimeLineMapper;
import com.basic.business.order.service.ITOrdersMainService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单主表服务实现类
 *
 * @author zhaoxy_jn@163.com
 */
@Service
public class TOrdersMainServiceImpl implements ITOrdersMainService {

    @Resource
    private ITOrdersMainMapper mainMapper;

    @Resource
    private ITOrdersDetailMapper detailMapper;

    @Resource
    private ITOrdersMergeMapper mergeMapper;

    @Resource
    private ITOrdersTimeLineMapper timeLineMapper;



    @Transactional
    @Override
    public int batchInsertOrUpdate(List<TOrdersMain> ordersMains) {
        if (CollectionUtils.isEmpty(ordersMains)) {
            return 0;
        }

        int cnt = mainMapper.batchInsertOrUpdate(ordersMains);
        if (cnt <= 0) {
            return cnt;
        }

        // 批量处理所有订单的明细和合并数据
        List<TOrdersTimeLine> timeLines = new ArrayList<>();
        List<TOrdersDetail> allDetails = new ArrayList<>();
        List<TOrdersMerge> allMerges = new ArrayList<>();

        ordersMains.forEach(ordersMain -> {
            // 处理订单明细
            if (!CollectionUtils.isEmpty(ordersMain.getDetailList())) {
                allDetails.addAll(ordersMain.getDetailList());
            }

            // 处理合并订单
            if (!CollectionUtils.isEmpty(ordersMain.getMergeList())) {
                allMerges.addAll(ordersMain.getMergeList());
            }

            // 构建时间线记录
            buildTimeLines(timeLines, ordersMain);
        });

        // 批量操作
        if (!allDetails.isEmpty()) {
            detailMapper.batchInsertOrUpdate(allDetails);
        }
        if (!allMerges.isEmpty()) {
            mergeMapper.batchInsertOrUpdate(allMerges);
        }
        if (!timeLines.isEmpty()) {
            timeLineMapper.batchInsert(timeLines);
        }

        return cnt;
    }

    private void buildTimeLines(List<TOrdersTimeLine> timeLines, TOrdersMain ordersMain) {
        // 下单时间
        TOrdersTimeLine timeLineCreated = TOrdersTimeLine.createTOrdersTimeLine(ordersMain);
        timeLineCreated.setOrderStatus("CREATED");
        timeLineCreated.setAccessTime(ordersMain.getCreated());
        timeLines.add(timeLineCreated);
        // 付款时间
        TOrdersTimeLine timeLinePayTime = TOrdersTimeLine.createTOrdersTimeLine(ordersMain);
        timeLinePayTime.setOrderStatus("PAY_TIME");
        timeLinePayTime.setAccessTime(ordersMain.getPayTime());
        timeLines.add(timeLinePayTime);
    }


    @Override
    public void deleteByMainSid(long mainSid) {
        mainMapper.deleteByMainSid(mainSid);
        detailMapper.deleteByMainSid(mainSid);
        mergeMapper.deleteByMainSid(mainSid);
    }



    @Override
    public Long getLastUpdTime() {
        return mainMapper.getLastUpdTime();
    }

}
